/**
 * 
 */
package com.kbcss.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


import com.kbcss.dao.DeptDao;
import com.kbcss.dto.Department;

/**
 * @author ibm123
 *
 */
@Controller
@RequestMapping(value="/adddept.spring")
public class CreateDeptController {
	@Autowired
	private DeptDao deptdao;
	public void setDeptDao(DeptDao deptdao){
        this.deptdao=deptdao;
    }
    
	@RequestMapping(method=RequestMethod.POST)
    public String storeDept(@ModelAttribute("department")Department department,HttpServletRequest request){
        String message="Department created successfully";
        try{
            deptdao.createDept(department.getDeptId(), department.getDeptName());

        }
        catch(Exception ex){
            message="Error:"+ex.getMessage();
        }
        request.setAttribute("message", message);
        return "results";

    }

	public ModelAndView list(HttpServletRequest request,

			HttpServletResponse response) throws Exception {

			ModelMap modelMap = new ModelMap();

			modelMap.addAttribute("deptList", deptdao.getAllDepartment());

			modelMap.addAttribute("department", new Department());

			return new ModelAndView("userForm", modelMap);

			}

}
